<?php
class menuController extends adminController{

	public function index(){
		$list=ApiHook('get_menu_index');
		$list=$this->sort_menu($list);
		echo $this->to_html($list,'管理首页');
	}

	
	public function category(){
		$list=ApiHook('get_menu_category');
		$list=$this->sort_menu($list);
		echo $this->to_html($list,'栏目管理');
	}

	public function content(){
		$model_list=model('model_manage')->model_list();
		if(!empty($model_list)){
			foreach ($model_list as $value) {
				if($value['list_type']){
					$list[$value['name'].'管理'] = array('url'=>url($value['model'].'/admin_content/index'),'sequence'=>0);
				}
			}
		}

		$new_list=ApiHook('get_menu_content');
		$new_list=$this->sort_menu($new_list);
		$list=array_merge((array)$list,(array)$new_list);
		echo $this->to_html($list,'内容管理');

		$form_list=api('form','form_list');
		$list2=array();
		if(!empty($form_list)){
			foreach ($form_list as $value) {
				$list2[$value['name']]=array('url'=>url('form/admin_list_content/index',array('fid'=>$value['id'])));
			}
		}
		echo $this->to_html($list2,'表单内容',false);
	}

	public function expand(){
		$list=ApiHook('get_menu_expand');
		$list=$this->sort_menu($list);
		echo $this->to_html($list,'扩展功能');
	}

	public function admin(){
		$list=ApiHook('get_menu_adminuser');
		$list=$this->sort_menu($list);
		echo $this->to_html($list,'后台用户');
		$list=ApiHook('get_menu_system');
		$list=$this->sort_menu($list);
		echo $this->to_html($list,'系统管理',false);
	}

	public function sort_menu($list=array()){
		if(empty($list)){
			return;
		}
		foreach ($list as $key => $value) {
				foreach($value as $v){  
			    $data[$v['name']]=$v;
			    $sequence[$v['name']] = $v['sequence'];
			}
		}
		array_multisort($sequence,SORT_ASC, $data);
		return $data;
	}

	public function to_html($data,$title,$top=true){
		if($top){
			$top_class='s-menu-hd';
		}else{
			$top_class='s-menu-hd2';
		}
		$html='
		<div class="m-menu-hd '.$top_class.' open"><div>'.$title.' </div></div>
			<ul class="m-menu-list s-menu-list">';
		$i=0;
		if(!empty($data)){
			foreach ($data as $name => $val) {
				$i++;
				if($i==1&&$top){
					$selected=' class="selected"';
				}else{
					$selected='';
				}
				$html.='<li><a href="javascript:;" data="'.$val['url'].'" '.$selected.' >'.$name.'</a></li>';
			}
		}
		$html.='</ul>
		';
		return $html;
	}
}